package com.brillilab.service.core.experiments.impl;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import com.brillilab.domain.vo.experiments.ExperimentPhaseProcessVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.common.alioss.OssFileEnum;
import com.brillilab.dao.mapper.experiments.ExperimentPhaseMapper;
import com.brillilab.domain.enums.common.BoolEnum;
import com.brillilab.domain.po.experiments.ExperimentPhase;
import com.brillilab.domain.po.system.FileManage;
import com.brillilab.domain.vo.experiments.ExperimentPhaseVo;
import com.brillilab.domain.vo.experiments.ExperimentStepVo;
import com.brillilab.service.core.experiments.IExperimentPhaseService;
import com.brillilab.service.core.system.IFileManageService;

@Service
@Transactional
public class ExperimentPhaseServiceImpl implements IExperimentPhaseService {

    @Resource
    private ExperimentPhaseMapper experimentPhaseMapper;
    @Resource
    private IFileManageService fileManageService;

    //**** WuMenghao start ****//
    @Override
    public ExperimentPhase selectById(Long experimentPhaseId) {
        return experimentPhaseMapper.selectById(experimentPhaseId);
    }

    @Override
    public ExperimentPhase selectExperimentFirstPhase(Long experimentId) {
        return experimentPhaseMapper.selectOne(
                new LambdaQueryWrapper<ExperimentPhase>()
                        .eq(ExperimentPhase::getExperimentId,experimentId)
                        .eq(ExperimentPhase::getSort,1)
                        .eq(ExperimentPhase::getIsDelete,BoolEnum.FALSE.getValue()));
    }

    @Override
    public int selectCount(Long experimentId) {
        return experimentPhaseMapper.selectCount(
                new LambdaQueryWrapper<ExperimentPhase>()
                        .eq(ExperimentPhase::getExperimentId,experimentId)
                        .eq(ExperimentPhase::getIsDelete,BoolEnum.FALSE.getValue()));
    }


	@Override
	public boolean deleteByExperimentId(Long experimentId) {
		return experimentPhaseMapper.logicallyDeleteBatchByExperimentId(experimentId) > 0;
	}

    @Override
    public int insertBatch(List<ExperimentPhase> newPhases) {
    	if(CollectionUtils.isNotEmpty(newPhases)){
    		return experimentPhaseMapper.insertBatch(newPhases);
		}
        return 0;
    }
    //****      end        ****//

    @Override
    public List<ExperimentPhaseVo> selectExperimentPhaseVo(Long experimentId) {
        return experimentPhaseMapper.selectExperimentPhaseVo(experimentId);
    }

    @Override
    public List<ExperimentPhaseVo> selectExperimentStepVoList(Long experimentId) {
    	List<ExperimentPhaseVo> phaseVoList = experimentPhaseMapper.selectExperimentPhaseVoList(experimentId);
    	if(!CollectionUtils.isEmpty(phaseVoList)) {
    		for (Iterator<ExperimentPhaseVo> iterator = phaseVoList.iterator(); iterator.hasNext();) {
				ExperimentPhaseVo phaseVo = iterator.next();
				List<ExperimentStepVo> stepVoList = phaseVo.getExperimentStepVoList();
				if(!CollectionUtils.isEmpty(stepVoList)) {
					for (Iterator<ExperimentStepVo> iterator2 = stepVoList.iterator(); iterator2.hasNext();) {
						ExperimentStepVo stepVo = (ExperimentStepVo) iterator2.next();
						List<FileManage> tipsFileList = fileManageService.selectList(OssFileEnum.TipFile, stepVo.getStepId());
						stepVo.setTipsFileList(tipsFileList);
						if(StringUtils.isBlank(stepVo.getTips()) && CollectionUtils.isEmpty(stepVo.getTipsFileList())) {
							iterator2.remove();
						}
					}
				}
				if(CollectionUtils.isEmpty(stepVoList)) {
					iterator.remove();
				}
			}
    	}
        return phaseVoList;
    }
    
  //**** LJ start ****//
	@Override
	public ExperimentPhase insert(ExperimentPhase experimentPhase) {
		 int insert = experimentPhaseMapper.insert(experimentPhase);
		 Assert.isTrue(insert == 1,"操作失败！");
		return experimentPhase;
	}

	@Override
	public boolean updateById(ExperimentPhase phase) {
		return experimentPhaseMapper.updateById(phase)==1;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<ExperimentPhase> selectList(Long experimentId) {
		 return experimentPhaseMapper.selectList(
	                new LambdaQueryWrapper<ExperimentPhase>()
	                        .eq(ExperimentPhase::getExperimentId,experimentId)
	                        .eq(ExperimentPhase::getIsDelete,BoolEnum.FALSE.getValue())
	                        .orderByAsc(ExperimentPhase::getSort));
	}

    @Override
    public List<ExperimentPhaseProcessVo> getExperimentPhaseVoOfProcess(Long experimentId) {
        return experimentPhaseMapper.selectExperimentPhaseVoOfProcess(experimentId);
    }

    //****   LJ   end        ****//
}
